﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Text.RegularExpressions;

namespace Gift.Utile
{
    public class CustomList : DataList
    {
        // Fields
        private const string AMP = "&amp;";
        private const string COMMA = "?";
        private int currentPageIndex;
        private IList dataSource;
        protected string emptyText;
        private string HTML1 = "<div class=page><span>";
        protected const string HTML2 = "</span>";
        protected const string HTML3 = "<p>";
        protected const string HTML4 = "</p></div>";
        private int itemCount;
        private const string KEY_PAGE = "page";
        private const string LINK_DISPLAY = "<a href=?page={0}>&nbsp;{1}&nbsp;</a>";
        private const string LINK_MORE = "| <a href=?page={0}>下一页 \x00bb &nbsp;</a>";
        private const string LINK_PREV = "<a href=?page={0}>&nbsp;\x00ab 上一页</a> |";
        private int pageSize = 12;
        private static readonly Regex RX = new Regex("&amp;page=d+", RegexOptions.Compiled);

        // Events
        public event DataGridPageChangedEventHandler PageIndexChanged;

        // Methods
        protected override void OnDataBinding(EventArgs e)
        {
            int num = this.CurrentPageIndex * this.pageSize;
            int num2 = Math.Min(this.pageSize, this.ItemCount - num);
            IList list = new ArrayList();
            for (int i = 0; i < num2; i++)
            {
                list.Add(this.dataSource[num + i]);
            }
            base.DataSource = list;
            base.OnDataBinding(e);
        }


        protected override void OnLoad(EventArgs e)
        {
            if (this.Visible)
            {
                string s = this.Context.Request["page"];
                int index = (s != null) ? (int.Parse(s) - 1) : 0;
                this.SetPage(index);
            }
        }

        protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e)
        {
            if (this.PageIndexChanged != null)
            {
                this.PageIndexChanged(this, e);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.ItemCount == 0)
            {
                writer.Write(this.emptyText);
            }
            else
            {
                base.Render(writer);
                writer.Write(GetOutHtml());
            }
          
        }

        string GetOutHtml()
        {
            StringBuilder sbOutPage = new StringBuilder();
            string input = this.Context.Request.Url.Query.Replace("?", "&amp;").Replace("&amp;page=" + Convert.ToInt32((int)(this.currentPageIndex + 1)), string.Empty).Replace("&amp;page=" + this.currentPageIndex, string.Empty).Replace("&amp;page=", string.Empty);
            input = RX.Replace(input, string.Empty);
          
            sbOutPage.Append(this.HTML1); ;
            sbOutPage.Append(string.Concat(new object[] { "&nbsp; 显示：共&nbsp;", Convert.ToInt64((int)(this.PageCount + 1)), "&nbsp;页&nbsp;", this.itemCount, "&nbsp 条" }));
            sbOutPage.Append("</span>");
            sbOutPage.Append("<p>");
            if (this.currentPageIndex > 0)
            {
                sbOutPage.Append(string.Format("<a href=?page={0}>&nbsp;\x00ab 上一页</a> |", this.currentPageIndex + input));
            }
            else
            {
                sbOutPage.Append("&nbsp;\x00ab 上一页 |");
            }
            if (((this.PageCount + 1) > 10) && ((this.currentPageIndex + 1) >= 10))
            {
                sbOutPage.Append(string.Format("<a href=?page={0}>&nbsp;{1}&nbsp;</a>", 1 + input, 1));
                sbOutPage.Append("...");
            }
            for (int i = 0; ((i <= 9) && (i <= this.PageCount)) && (((this.currentPageIndex - (this.currentPageIndex % 10)) + i) <= this.PageCount); i++)
            {
                if (((this.currentPageIndex - (this.currentPageIndex % 10)) + i) == this.currentPageIndex)
                {
                    sbOutPage.Append("&nbsp;<b>" + (this.currentPageIndex + 1) + "</b>&nbsp;");
                }
                else
                {
                    sbOutPage.Append(string.Format("<a href=?page={0}>&nbsp;{1}&nbsp;</a>", (((this.currentPageIndex - (this.currentPageIndex % 10)) + i) + 1) + input, ((this.currentPageIndex - (this.currentPageIndex % 10)) + i) + 1));
                }
            }
            if (((this.PageCount + 1) > 10) && ((this.currentPageIndex + 1) <= 10))
            {
                sbOutPage.Append("...");
                sbOutPage.Append(string.Format("<a href=?page={0}>&nbsp;{1}&nbsp;</a>", (this.PageCount + 1) + input, this.PageCount + 1));
            }
            if (this.currentPageIndex < this.PageCount)
            {
                sbOutPage.Append(string.Format("| <a href=?page={0}>下一页 \x00bb &nbsp;</a>", (this.currentPageIndex + 2) + input));
            }
            else
            {
                sbOutPage.Append("|&nbsp;下一页 \x00bb&nbsp;");
            }
            sbOutPage.Append("</p></div>");
            sbOutPage.Append("</p></div>");

        
            return sbOutPage.ToString();
        }

        public string   BuilderOutPageHeaderHtml
        {
            get
            {
                string input = this.Context.Request.Url.Query.Replace("?", "&amp;").Replace("&amp;page=" + Convert.ToInt32((int)(this.currentPageIndex + 1)), string.Empty).Replace("&amp;page=" + this.currentPageIndex, string.Empty).Replace("&amp;page=", string.Empty);
                input = RX.Replace(input, string.Empty);
                input = input.Replace("&amp;order=sale", string.Empty);
                input = input.Replace("&amp;order=price", string.Empty);
                input = input.Replace("&amp;order=time", string.Empty);
                string sLiClass1 = string.Empty;
                string sLiClass2 = string.Empty;
                string sLiClass3 = string.Empty;
                string sArrow1 = string.Empty;
                string sArrow2 = string.Empty;
                if (string.IsNullOrEmpty(this.Context.Request["order"]) || this.Context.Request["order"] == "sale")
                {
                    if(!string.IsNullOrEmpty(this.Context.Request["order"]))
                    {
                        if (ArrowDirectionSale == "&darr;")
                        {
                            ArrowDirectionSale = "&uarr;";
                        }
                        else
                        {
                            ArrowDirectionSale = "&darr;";
                        }
                    }
                    sArrow1 = ArrowDirectionSale;
                    sLiClass1 = "class='currentPage'";
                    sArrow2 = string.Empty;
                }
                else if (this.Context.Request["order"] == "price")
                {
                    if (ArrowDirectionPrice == "&darr;")
                    {
                        ArrowDirectionPrice = "&uarr;";
                    }
                    else
                    {
                        ArrowDirectionPrice = "&darr;";
                    }
                    sArrow2 = ArrowDirectionPrice;
                    sArrow1 = string.Empty;
                    sLiClass2 = "class='currentPage'";
                }
               else if (this.Context.Request["order"] == "time")
                {
                    sLiClass3 = "class='currentPage'";
                }
                StringBuilder sbPageHeader = new StringBuilder();
                sbPageHeader.Append("  <div style=\" width:100%;border-bottom:2px #BE0000 solid;margin-top:50px; text-align:left;  height:19px; padding-left:5px; float:left\">");
                sbPageHeader.Append("  <div>排序</div>");
                sbPageHeader.Append("   <div id=\"orderMenu\">");
                sbPageHeader.Append("  <ul>");
                sbPageHeader.Append(string.Format("  <li {1}><a href=?order=sale{0}>{4}销  量</a></li><li {2}><a href=?order=price{0}>{5}价  格</a></li><li {3}><a href=?order=time{0}>上价时间</a></li>", input, sLiClass1, sLiClass2, sLiClass3, sArrow1, sArrow2));
                sbPageHeader.Append("  </ul>");
                sbPageHeader.Append("  </div>");

                sbPageHeader.Append(" </div>");

                sbPageHeader.Append(GetOutHtml().Replace("class=page", "class=pageHeader"));
                return sbPageHeader.ToString();
                //System.Web.UI.HtmlControls.HtmlGenericControl divHtml = new System.Web.UI.HtmlControls.HtmlGenericControl();
                //divHtml.InnerHtml = sbPageHeader.ToString();
            }

        }

        public void SetPage(int index)
        {
            this.OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
        }

       public string ArrowDirectionSale
        {
            get
            {
                if (this.Context.Session["ArrowDirectionSale"] == null)
                {
                    return "&darr;";
                }else
                {
                    return this.Context.Session["ArrowDirectionSale"].ToString();
                }
            }
            set
            {
                this.Context.Session["ArrowDirectionSale"] = value;
            }
        }

       public string ArrowDirectionPrice
       {
           get
           {
               if (this.Context.Session["ArrowDirectionPrice"] == null)
               {
                   return "&darr;";
               }
               else
               {
                   return this.Context.Session["ArrowDirectionPrice"].ToString();
               }
           }
           set
           {
               this.Context.Session["ArrowDirectionPrice"] = value;
           }
       }

        // Properties
        public virtual int CurrentPageIndex
        {
            get
            {
                return this.currentPageIndex;
            }
            set
            {
                this.currentPageIndex = value;
            }
        }

        public override object DataSource
        {
            set
            {
                try
                {
                    this.dataSource = (IList)value;
                    this.ItemCount = this.dataSource.Count;
                }
                catch
                {
                    this.dataSource = null;
                    this.ItemCount = 0;
                }
            }
        }

        public string EmptyText
        {
            set
            {
                this.emptyText = value;
            }
        }

        protected virtual int ItemCount
        {
            get
            {
                return this.itemCount;
            }
            set
            {
                this.itemCount = value;
            }
        }

        protected int PageCount
        {
            get
            {
                return ((this.ItemCount - 1) / this.pageSize);
            }
        }

        public int PageSize
        {
            get
            {
                return this.pageSize;
            }
            set
            {
                this.pageSize = value;
            }
        }
    }


}
